package com.yuqing.sentimentapp.yqlist.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.event.CommonEvent;
import com.yuqing.sentimentapp.home.bean.ReplyBean;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.utils.ToastUtil;
import com.yuqing.sentimentapp.yqlist.activity.YQReportActivity;
import com.yuqing.sentimentapp.yqlist.bean.YqReportResultBean;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by shenhe on 2019/4/19.
 */
public class OtherTemplateFragment extends BaseFragment {
    @BindView(R.id.et_url)
    EditText mEtUrl;
    @BindView(R.id.et_address)
    EditText mEtAddress;
    @BindView(R.id.et_remark)
    EditText mEtRemark;
    @BindView(R.id.btn_report)
    Button mBtnReport;
    private String linkId;
    private String title;
    private String url;
    private String address;
    private String content;
    private String remark;
    private String replyId;
    private boolean isReportAgain;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_other_template;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initParamsData();
    }

    private void initParamsData() {
        linkId = ((YQReportActivity) getActivity()).getYqLinkId();
        title = ((YQReportActivity) getActivity()).getYqTitle();
        url = ((YQReportActivity) getActivity()).getYqUrl();
        content = ((YQReportActivity) getActivity()).getYqContent();

        mEtUrl.setText(url);
    }
    @Override
    protected void initPresenter() {

    }

    @OnClick(R.id.btn_report)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_report:
                address = mEtAddress.getText().toString().trim();
                remark = mEtRemark.getText().toString().trim();

                Map<String, String> param = new HashMap<>();
                param.put("reply.replyType", "C");
                if (isReportAgain) {
                    param.put("reply.id", replyId);
                }
                param.put("reply.linkId", linkId == null ? "" : linkId);
                param.put("reply.title", title == null ? "" : title);
                param.put("reply.url", url == null ? "" : url);
                param.put("reply.isVerified", "0");
                param.put("reply.isAlarm", "0");
                param.put("reply.remark", remark == null ? "" : remark);
                param.put("reply.address", address == null ? "" : address);
                param.put("reply.content", content == null ? "" : content);
                ModuleManager.getInstance().postReportDataResult(getActivity(), param, isReportAgain)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<YqReportResultBean>() {
                            @Override
                            protected void hideDialog() {
                                stopProgressDialog();
                            }

                            @Override
                            protected void showDialog() {
                                startProgressDialog();
                            }

                            @Override
                            protected void onSuccess(YqReportResultBean baseBean) {
                                ToastUtil.showShort("上报成功");
                                EventBus.getDefault().post(new CommonEvent(-1 , ((YQReportActivity) getActivity()).getPosition() + "", baseBean));
                                getActivity().finish();
                            }

                            @Override
                            protected void onFailure(String message) {
                                ToastUtil.showShort(message);
                            }
                        });
                break;
        }
    }

    public void updateData(ReplyBean reply, int state) {
        isReportAgain = state == 1;
        replyId = reply.getId();
        remark = reply.getRemark();
        address = reply.getAddress();
        mEtAddress.setText(address);
        mEtRemark.setText(remark);
        mBtnReport.setText(state == 1 ? "重报" : "上报");
    }
}
